package cn.yunhe.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import cn.yunhe.Utils.PageBean;
import cn.yunhe.bean.*;
import cn.yunhe.service.OrdersService;
import cn.yunhe.service.ProductService;
import org.apache.ibatis.annotations.Select;
import org.ietf.jgss.Oid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class ordersController {
	@Autowired
	private OrdersService ordersService;
	@Autowired
	private ProductService productService;
	@RequestMapping("/toOrder")
	public String myOrder(HttpServletRequest request,Model model) {
		User loginUser = (User) request.getSession().getAttribute("loginUser");
		Orders orders = new Orders();
		Cart cart = (Cart) request.getSession().getAttribute("cart");
		Collection<CartItem> cartItems1 = cart.getCartItems();

		if(loginUser==null){
			model.addAttribute("message", "对不起您还没有登录");
			return "msg";
		}
//		0表示没有付款、1表示已付款即将发货 2表示确认收货 3表示交易成功
//		orders.setState(0);
//		orders.setOrderTime(new Date());
//		orders.setUid(loginUser.getUid());
//		orders.setMoney(cart.getTotale());
//		ordersService.toOrder(orders);
		List<Orders> arrayList = new ArrayList();
		List list = new ArrayList();
		for (CartItem i : cartItems1){
			Orders orders1 = new Orders();
			orders1.setMoney(i.getProduct().getShopPrice());
			orders.setState(0);
			orders.setOrderTime(new Date());
			orders.setUid(loginUser.getUid());
			arrayList.add(orders1);
			Integer pid = i.getProduct().getPid();
			list.add(pid);
		}
		model.addAttribute("gee",arrayList);
		request.getSession().setAttribute("pid",list);

		Map<Integer, CartItem> cartItems = cart.getCartItem();
		for (Entry<Integer, CartItem> entry : cartItems.entrySet()) {
			CartItem cartItem = entry.getValue();
			Orderitem orderitem = new Orderitem();
			orderitem.setProduct(cartItem.getProduct());
			orderitem.setCount(cartItem.getCount());
			orderitem.setPid(cartItem.getProduct().getPid());
			orderitem.setSubtotal(cartItem.getSubtotle());
			orderitem.setOid(orders.getOid());
			orders.getOiList().add(orderitem);
			ordersService.toOrderItem(orderitem);
		}
		cart.clearCart();
		request.getSession().setAttribute("orders", orders);
		return "order";
	}
	// 查询我的订单
	@RequestMapping(value="/myOrder")
	public String myOrder(@RequestParam int page, Model model,HttpServletRequest request){
		User loginUser = (User) request.getSession().getAttribute("loginUser");
		PageBean<Orders> pageBean = ordersService.findOrderByUidAndPage(page,loginUser.getUid());
		model.addAttribute("pageBean", pageBean);
		return "orderList";
	}
  //payOrderAganin
	@RequestMapping("/payOrderAganin")
	public String payOrderAganin(@RequestParam int oid,HttpServletRequest request){
		Orders noPayOrder = ordersService.findOrderByOid(oid);
		request.getSession().setAttribute("orders", noPayOrder);
		return "order";
	}
	// 为订单付款
	@PostMapping("/payOrder")
	public String payOrder(Integer[] pdd,String receiveinfo,String accepter,String phonum,HttpServletRequest request,Model model) throws ParseException {
		User loginUser = (User) request.getSession().getAttribute("loginUser");
		for (int i = 0 ;i <pdd.length;i++) {
			Orders orders = new Orders();
			Product product = productService.finbProductByPid(pdd[i]);
			orders.setReceiveinfo(receiveinfo);
			orders.setPhonum(phonum);
			orders.setState(0);
			orders.setUid(loginUser.getUid());
			Date date = new Date();
			SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			String format = ft.format(date);
			Date parse = ft.parse(format);
			orders.setOrderTime(parse);
			orders.setAccepter(accepter);
			orders.setMoney(product.getShopPrice());
			ordersService.payOrder(orders,request.getSession());
		}
		request.getSession().setAttribute("zhcg",true);
//		model.addAttribute("zhcg",true);
		return "redirect:http://localhost:8085/";
	}
	// 确认收货
			@RequestMapping("/updateState")
			public String updateState(@RequestParam int oid ) throws Exception {
				ordersService.updateOrderStatus(oid, 3);
				return "redirect:myOrder?page=1";
			}


}
